<?php

namespace App\Admin\Controllers;

use App\Models\ExcelRule;
use App\Models\ExcelTemplate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ExcelTemplateController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '导出模版';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ExcelTemplate());

        $grid->column('id', __('ID'));
        $grid->column('title', __('模版名称(点击进入)'))->display(function ($title, $column) {
            return $column->link('/admin/form/setting?template_id=' . $this->id, '_self');
        });
        $grid->column('sheet_num', __('工作区数量'));
        $grid->column('create_time', __('创建时间'));
        $grid->column('update_time', __('更新时间'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ExcelTemplate::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('模版名称'));
        $show->field('sheet_num', __('工作区数量'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ExcelTemplate());

        $form->text('title', __('模版名称'));
        $form->number('sheet_num', __('工作区数量'))->default(5)->min(1)->max(10);

        //保存后回调
        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $sheetNum = $form->model()->sheet_num;
            if ($sheetNum) {
                $sheetList = range(1, $sheetNum);
                $sheetArr = [];
                foreach ($sheetList as $item) {
                    $sheetName = 'sheet' . $item;
                    $sheetArr[] = $sheetName;
                    $data = ExcelRule::query()->where('type', $sheetName)->where('template_id', $id)->where('is_del', 0)->first();
                    if (empty($data)) {
                        ExcelRule::query()->create([
                            'title'       => $sheetName,
                            'type'        => $sheetName,
                            'template_id' => $id,
                        ]);
                    }
                }

                ExcelRule::query()->whereNotIn('type', $sheetArr)->where('template_id', $id)->where('is_del', 0)->update(['is_del' => 1]);
            }
        });

        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`提交`按钮
//            $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        return $form;
    }
}
